package com.gongpingjialoan.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.BuyCarVipActivity;
import com.gongpingjialoan.activity.car.SellCarHistoryActivity;
import com.gongpingjialoan.activity.fb.MyPushIntentService;
import com.gongpingjialoan.activity.loans.LoanIndexActivity;
import com.gongpingjialoan.activity.main.CarOrderCheckActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.activity.tool.WebViewActivity;
import com.gongpingjialoan.activity.vip.VIPDetailActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.BannerBean;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.data.LoanOrderData;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.imageload.ImageLoad;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.GPSUtil;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.utility.Utils;
import com.gongpingjialoan.widget.autoviewpagers.LoopPagerAdapter;
import com.gongpingjialoan.widget.autoviewpagers.RollPagerView;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by Administrator on 2016/4/8.
 */
public class LooperAdapter extends LoopPagerAdapter {

    private List<BannerBean.DataEntity.EvalEntity> dataEntities;

    private Context mContext;

    private FragmentManager fragmentManager;

    private boolean isStop = false;

    private boolean isBaoxian = false;

    private boolean istop = false;

    public LooperAdapter(RollPagerView viewPager, List<BannerBean.DataEntity.EvalEntity> dataEntities, Context mContext, FragmentManager fragmentManager, boolean isStop) {
        super(viewPager);
        this.dataEntities = dataEntities;
        this.mContext = mContext;
        this.isStop = isStop;
        this.fragmentManager = fragmentManager;
    }


    public LooperAdapter(RollPagerView viewPager, List<BannerBean.DataEntity.EvalEntity> dataEntities, Context mContext, FragmentManager fragmentManager, boolean isStop, boolean istop) {
        super(viewPager);
        this.dataEntities = dataEntities;
        this.mContext = mContext;
        this.isStop = isStop;
        this.fragmentManager = fragmentManager;
        this.istop = istop;
    }

    public void setIsBaoxian(boolean isBaoxian) {
        this.isBaoxian = isBaoxian;
    }

    @Override
    public View getView(ViewGroup container, final int position) {
        ImageView view = new ImageView(container.getContext());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("".equals(dataEntities.get(position).getUrl())) return;
                if (isStop) {
                    StepMonitor.getInstance().addMonitor(new Monitor("click", "eval_banner", System.currentTimeMillis(), 1.0f, dataEntities.get(position).getId()));
                }
                if (dataEntities.get(position).getUrl().startsWith("http")) {
                    Intent intent = new Intent(mContext, WebViewActivity.class);
                    if (isStop && MainActivity.main != null && MainActivity.main.assessmentFragment.isAdded()) {
                        intent.putExtra("from", "bannar");
                        MainActivity.main.assessmentFragment.stopPlay();
                    }
                    String url = dataEntities.get(position).getUrl();
                    String name = dataEntities.get(position).getName();
                    String city = Utils.getCityName(mContext.getApplicationContext());
                    if ("贷款买车生活0压力".equals(name) && ("苏州".equals(city) || "成都".equals(city))) {
                        if (GPJApplication.getInstance().isLogin()) {
                            getOrderId();
                        } else {
                            MainActivity.main.assessmentFragment.loginforBannerLoan();
                        }


                    } else {
                        intent.putExtra("url", url);
                        intent.putExtra("content", name);
                        mContext.startActivity(intent);
                    }

                } else if (dataEntities.get(position).getUrl().startsWith("app")) {
                    String app = dataEntities.get(position).getUrl().substring(6);
                    if ("promotionlist".equals(app)) {
                        Intent intent = new Intent(mContext, MainActivity.class);
                        intent.putExtra("type", MyPushIntentService.PROMINTIONLIST);
                        mContext.startActivity(intent);
                    } else if ("sellcar".equals(app)) {
                        Intent intent = new Intent(mContext, MainActivity.class);
                        intent.putExtra("type", "sell_car");
                        mContext.startActivity(intent);
                    } else if ("vipbuy".equals(app)) {
                        Intent intent = new Intent(mContext, BuyCarVipActivity.class);
                        mContext.startActivity(intent);
                    } else if ("sellcarhistory".equals(app)) {
                        Intent intent = new Intent(mContext, SellCarHistoryActivity.class);
                        mContext.startActivity(intent);
                    } else if ("market".equals(app)) {
                        Intent intent = new Intent(mContext, MainActivity.class);
                        intent.putExtra("type", MyPushIntentService.MARKET);
                        mContext.startActivity(intent);

                    } else if ("vip-charge".equals(app)) {
                        Intent intent = new Intent(mContext, VIPDetailActivity.class);
                        intent.putExtra("from", "eval_banner");
                        mContext.startActivity(intent);

                    } else if ("detection".equals(app)) {
                        Intent intent = new Intent(mContext, CarOrderCheckActivity.class);
                        mContext.startActivity(intent);
                    } else if ("loan".equals(app)) {

                        Intent intent = new Intent(mContext, LoanIndexActivity.class);
                        mContext.startActivity(intent);
//                        String url = dataEntities.get(position).getUrl();
//                        String name = dataEntities.get(position).getName();
//                        String city = Utils.getCityName(mContext.getApplicationContext());
//
//                        if(("苏州".equals(city) || "成都".equals(city))){
//                            if (GPJApplication.getInstance().isLogin()) {
//                                getOrderId();
//                            }else{
//                                Intent intent = new Intent();
//                                intent.setClass(mContext, LoginActivity.class);
//                                MainActivity.main.assessmentFragment.startActivityForResult(intent, AssessmentFragment.REQUEST_BANNER_LOAN);
//                            }
//
//
//                        }else{
//                            Intent intent = new Intent(mContext, WebViewActivity.class);
//                            intent.putExtra("url",url);
//                            intent.putExtra("content", name);
//                            mContext.startActivity(intent);
//                        }
                    }
                }
            }
        });
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        if (istop) {
            Glide.with(mContext).load(dataEntities.get(position).getImg()).override(700, 300).placeholder(R.drawable.trans).error(R.drawable.trans).into(view);
        } else {
            if (isBaoxian) {
                ImageLoad.LoadImage(view, dataEntities.get(position).getImg(), R.drawable.baoxian_example, R.drawable.baoxian_example, mContext);
            } else {
                ImageLoad.LoadImage(view, dataEntities.get(position).getImg(), R.drawable.trans, R.drawable.trans, mContext);
            }
        }


        return view;
    }

    private void gotoWeb(String order_id, String user_id) {

        Intent intent = new Intent(mContext, WebViewActivity.class);
        String url = "http://www.eflashloan.com/eflashapp/index.html#" + order_id;
        intent.putExtra("url", url);
        intent.putExtra("order_id", order_id);
        intent.putExtra("user_id", user_id);
        intent.putExtra("vehicle_value", "");
        intent.putExtra("access_ip", GPSUtil.getLocalIpAddress());
        intent.putExtra("source", 2);
        intent.putExtra("loadType", "post");
        intent.putExtra("city", Utils.getCityName(mContext.getApplicationContext()));

        if (isStop && MainActivity.main != null && MainActivity.main.assessmentFragment.isAdded()) {
            intent.putExtra("from", "bannar");
            MainActivity.main.assessmentFragment.stopPlay();
        }
        intent.putExtra("url", url);
        intent.putExtra("type", "eval_flashloan");
        intent.putExtra("content", "贷款买车");

        mContext.startActivity(intent);
        StepMonitor.getInstance().addMonitor(new Monitor("click", "eval_flashloan", System.currentTimeMillis(), 1f));


    }

    public void getOrderId() {
        NetDataJson netDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDataJsonUpdate(String json) {
                Gson gson = new Gson();
                LoanOrderData loanOrderData = gson.fromJson(json, LoanOrderData.class);
                String order_id = loanOrderData.getData().getOrder_no();
                String user_id = loanOrderData.getData().getUser_id();
                gotoWeb(order_id, user_id);
            }
        }, 1);
        netDataJson.setUrl(NetDataJson.LOAN_SERVER_DOMAIN + API.loan_get_order_id);
        netDataJson.setUseOtherDomain(true);
        netDataJson.addParam("source", 2);
        netDataJson.request("post");

    }

    @Override
    protected int getRealCount() {

//        List<BannerBean.DataEntity.EvalEntity> removeList = new ArrayList();
//
//        for (int i = 0; i < dataEntities.size(); i++) {
//            BannerBean.DataEntity.EvalEntity en = dataEntities.get(i);
//            if (TextUtils.isEmpty(en.getUrl())) {
//                removeList.add(en);
//            }
//        }
//        dataEntities.removeAll(removeList);

        return dataEntities.size();
    }
}
